<script>


import axios from "axios";
import UploadPhoto from "@/components/dialog/UploadPhoto.vue";
import CarouselMap from "@/components/dialog/CarouselMap.vue";
import AlbumEdit from "@/components/dialog/AlbumEdit.vue";
import RecycleBin from "@/components/dialog/RecycleBin.vue";
import FriendData from "@/components/dialog/FriendData.vue";

export default {
  name: "PhotoView",
  components: {FriendData, RecycleBin, AlbumEdit, CarouselMap, UploadPhoto},
  data() {
    return {
      menu: 1,
      create_dialog: false,
      new_album_name: "",
      no_data: false,
      album_loading: false,
      upload_photo: false,
      album_list: [{
        id: 0,
        name: "",
        cover: "",
        count: 0
      }],
      photo_list: [],
      carousel_map: false,
      album_name: "",
      album_id: "",
      album_edit: false,
      recycle_bin: false,
      visitor_list: [],
      data_dialog:false,
      friendID:""
    }
  },
  methods: {
    getVisitor() {
      axios.get("/api/notification/visitor")
          .then(res => {
            if (res.data.code === 0) {
              this.visitor_list = res.data.data
            } else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    },
    menuClick(e, j) {
      let div = document.getElementsByClassName('p_menu')
      for (let i = 0; i < div.length; i++) {
        div[i].classList.remove('p_menu_c')
      }
      e.target.classList.add('p_menu_c')
      this.menu = j
    },
    handleCommand(command) {
      this.getAlbumList(command)
      let i = document.getElementsByClassName('i')
      for (let j = 0; j < i.length; j++) {
        i[j].classList.remove('el-icon-check')
      }
      i[command].classList.add('el-icon-check')
    },
    showXlBtn(e) {
      let div = e.target.firstChild.firstChild
      if (div.style.display === 'block')
        div.style.display = 'none'
      else
        div.style.display = 'block'
    },
    showSpKs(e) {
      let div = e.target.lastChild
      if (div.style.display === 'block')
        div.style.display = 'none'
      else
        div.style.display = 'block'
    },
    create() {
      if (this.new_album_name !== '') {
        let data = new FormData
        data.append("name", this.new_album_name)
        axios.post("/api/album/", data)
            .then(res => {
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                this.create_dialog = false
                this.handleCommand(1)
                this.new_album_name = ""
              } else {
                this.$message.warning(res.data.msg)
              }
            }).catch(err => this.$message.error(err)
        )
      }

    },
    getAlbumList(type) {
      this.menu = 1
      this.album_loading = true
      axios.get(`/api/album/list/${type}`)
          .then(res => {
            if (res.data.code === 0) {
              this.album_loading = false
              this.album_list = res.data.data
              this.no_data = this.album_list.length === 0;
            } else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    },
    getAlbumDetail(index) {
      if (this.album_list[index].count === 0) {
        this.$message.info("请先上传照片")
      } else {
        this.album_name = this.album_list[index].name
        this.album_id = this.album_list[index].id
        this.carousel_map = true
      }
    },
    delAlbum(id) {
      this.$alert('确定删除该相册吗', '提示', {
        confirmButtonText: '确定',
        callback: () => {
          axios.get(`/api/album/del/${id}`)
              .then(res => {
                if (res.data.code === 0) {
                  this.getAlbumList(0)
                  this.$message.success(res.data.msg)
                } else {
                  this.$message.warning(res.data.msg)
                }
              }).catch(err => this.$message.error(err))
        }
      });

    },
    setStatus(index) {
      let status = 0
      if (this.album_list[index].status === 0) {
        status = 1
      }
      axios.get(`/api/album/status/${this.album_list[index].id}/${status}`)
          .then(() => this.album_list[index].status = status)
          .catch(err => this.$message.error(err))
    },
    setUpload() {
      this.getAlbumList(0)
      this.upload_photo = false
    },
    getPhotoList() {
      this.album_loading = true
      axios.get("/api/photo/list")
          .then(res => {
            if (res.data.code === 0) {
              this.album_loading = false
              this.photo_list = res.data.data
            } else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    },
    showBigPic(src) {
      let big = document.getElementsByClassName('big_pic')[0]
      big.parentNode.style.display = 'flex'
      big.src = src
    },
    delPhoto(item) {
      this.$alert('确定将该照片移入回收站吗', '提示', {
        confirmButtonText: '确定',
        callback: () => {
          for (let i = 0; i < item.ids; i++) {
            axios.get(`/api/photo/del/${item.ids[i]}`)
                .then(res => {
                  if (res.data.code === 0) {
                    this.getPhotoList()
                    this.$message.success(res.data.msg)
                  } else {
                    this.$message.warning(res.data.msg)
                  }
                }).catch(err => this.$message.error(err))
          }
        }
      });
    }
  },
  watch: {
    menu: {
      immediate: true,
      handler(value) {
        switch (value) {
          case 1:
            this.getAlbumList(0);
            this.getVisitor()
            break
          case 2:
            if (this.album_list.length > 0) {
              this.getPhotoList()
            } else {
              this.$message.info("暂无上传记录")
              this.menu = 1
            }
            break
            // case 3:
            //   break
        }
      }
    }
  },
}
</script>

<template>
  <div class="cont_box p_cont_box">
    <div class="data_dialog">
      <el-dialog :visible.sync="data_dialog" title="她说梦里全都有的主页" top="10vh" width="70%" destroy-on-close>
        <friend-data :id="friendID"></friend-data>
      </el-dialog>
    </div>
    <el-dialog :visible.sync="create_dialog" title="创建相册" top="20vh" width="30%" destroy-on-close>
      <div class="hf_box">
        <el-input placeholder="输入要创建的相册名" v-model="new_album_name" :maxlength="10"></el-input>
      </div>
      <button class="pl_btn hf_btn" @click="create">新建</button>
    </el-dialog>

    <el-dialog :visible.sync="upload_photo" title="上传照片" top="10vh" width="50%" destroy-on-close>
      <upload-photo :album_list="album_list" @set="setUpload"></upload-photo>
    </el-dialog>

    <el-dialog :visible.sync="album_edit" :title="'《'+album_name+'》'" top="10vh" width="60%" destroy-on-close
               @close="album_id='';album_name='';getAlbumList(0)">
      <album-edit :id="album_id"></album-edit>
    </el-dialog>
    <el-dialog :visible.sync="recycle_bin" title="回收站" top="10vh" width="60%" destroy-on-close>
      <recycle-bin :recycle_bin="recycle_bin" @close="getAlbumList"></recycle-bin>
    </el-dialog>

    <div class="carousel">
      <el-dialog :visible.sync="carousel_map" :title="'《'+album_name+'》'" top="3vh" width="80%" destroy-on-close
                 @close="album_id='';album_name=''">
        <carousel-map :id="album_id"></carousel-map>
      </el-dialog>
    </div>

    <div class="p_cont_top">
      <div class="p_cont_top_left">
        <div :class="menu === 1?'p_menu p_menu_c':'p_menu'" @click="menu = 1">
          相册
        </div>
        <div :class="menu === 2?'p_menu p_menu_c':'p_menu'" @click="menu = 2">
          照片
        </div>
        <!--        <div class="p_menu" @click="menuClick($event,3)">-->
        <!--          视频-->
        <!--        </div>-->
      </div>
      <div class="p_cont_top_right">
            <span @click="recycle_bin=true">
              <i class="el-icon-delete"></i>
              回收站
            </span>
      </div>
    </div>
    <div class="p_cont">
      <div class="no_value" v-show="no_data">
        <div>
          <div>不问过去，无论未来</div>
          <img :src="require('@/assets/img/99.png')" alt="">
        </div>
        <button @click="menu===3?'':create_dialog=true">{{ menu === 3 ? '上传视频' : '新建相册' }}</button>
      </div>

      <div class="xc_box" v-if="menu===1" v-loading="album_loading">
        <div class="p_ct" v-show="!no_data">

          <button class="up_btn" @click="upload_photo=true"><i class="iconfont icon-tubiaolunkuo-"></i>上传图片</button>

          <el-button class="btn" @click="create_dialog=true">创建相册</el-button>
          <el-dropdown @command="handleCommand" placement="bottom-start">
            <el-button class="btn">展示设置</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="0">最近上传在前<i class="i el-icon-check"
                                                            style="margin-left: 5px;color: #409EFF"></i>
              </el-dropdown-item>
              <el-dropdown-item :command="1">最近创建在前<i class="i" style="margin-left: 5px;color: #409EFF"></i>
              </el-dropdown-item>
              <el-dropdown-item :command="2">最近创建在后<i class="i" style="margin-left: 5px;color: #409EFF"></i>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="p_pic_box">
          <el-card class="pic_card" shadow="hover" v-for="(item,index) in album_list" :key="index"
                   @click.native="getAlbumDetail(index)">
            <div class="pic_card_img_box">
              <img alt="" :src="item.cover!==''?item.cover:require('@/assets/img/无图片.png')"/>
              <div class="mask" @mouseenter="showXlBtn" @mouseleave="showXlBtn">
                <el-dropdown placement="bottom-end">
                  <div class="xl_btn"><i class="iconfont icon-a-xiala2"></i></div>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="album_id=item.id;album_name=item.name;album_edit=true">编辑
                    </el-dropdown-item>
                    <el-dropdown-item @click.native="setStatus(index)">{{ item.status === 1 ? '公开相册' : '私密相册' }}
                    </el-dropdown-item>
                    <el-dropdown-item style="color:#f44336" @click.native="delAlbum(item.id)">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <div class="pic_zs">{{ item.count }}</div>
              </div>
            </div>
            {{ item.name }}<i v-show="item.status === 1" class="el-icon-lock"
                              style="font-size: 18px;color: #409EFF;margin-left: 5px"></i>
          </el-card>
        </div>
        <div class="fk_box">
          <div class="fk_box_top">
            最近访客<span>访客总数: {{ visitor_list.length }}</span>
          </div>
          <div class="fk_tx_box" v-if="visitor_list.length !== 0">
            <div class="fk_tx" v-for="(item,index) in visitor_list" :key="index">
              <el-avatar :src="item.avatar"
                         shape="square"></el-avatar>
              <div class="mask" @click="friendID=item.id;data_dialog=true">
                <div class="fk_nick">
                  {{ item.nickname.length > 5 ? item.nickname.substring(0, 5) + '...' : item.nickname }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="zp_box" v-if="menu===2">
        <div v-for="(item,index) in photo_list" :key="index">
          <div class="zp_left" v-html="item.time">

          </div>
          <div class="zp_right">
            上传到相册：<a @click="album_id=item.id;album_name=item.albumName;carousel_map=true">{{ item.albumName }}</a>
            <div class="many_pic" v-if="item.src.length>1">
              <div class="m_pic_box" v-for="(r_item,r_index) in item.src" :key="r_index">
                <img :src="r_item" alt="" @click="showBigPic(r_item)">
              </div>
            </div>

            <div class="one_pic" v-else>
              <img :src="item.src[0]" alt="" @click="showBigPic(item.src[0])">
            </div>

            <div class="del_jl"></div>
          </div>

        </div>


      </div>

      <!--      <div class="sp_box" v-if="menu===3">-->
      <!--        <button class="up_btn"><i class="el-icon-video-camera"></i>上传视频</button>-->
      <!--        <div>-->
      <!--          <div class="sp_item_box">-->
      <!--            <div class="sp_item" @mouseenter="showSpKs" @mouseleave="showSpKs">-->
      <!--              <div class="mask">-->
      <!--                <img class="bf_img" :src="require('@/assets/img/24gf-play (1).png')" alt=""/>-->
      <!--              </div>-->
      <!--            </div>-->
      <!--            <div class="sp_title">2023-1-1</div>-->

      <!--          </div>-->
      <!--          <div class="sp_item_box">-->
      <!--            <div class="sp_item" @mouseenter="showSpKs" @mouseleave="showSpKs">-->
      <!--              <div class="mask">-->
      <!--                <img :src="require('@/assets/img/24gf-play (1).png')" alt=""/>-->
      <!--              </div>-->
      <!--            </div>-->
      <!--            <div class="sp_title">2023-1-1</div>-->

      <!--          </div>-->
      <!--          <div class="sp_item_box">-->
      <!--            <div class="sp_item" @mouseenter="showSpKs" @mouseleave="showSpKs">-->
      <!--              <div class="mask">-->
      <!--                <img :src="require('@/assets/img/24gf-play (1).png')" alt=""/>-->
      <!--              </div>-->
      <!--            </div>-->
      <!--            <div class="sp_title">2023-1-1</div>-->

      <!--          </div>-->
      <!--          <div class="sp_item_box">-->
      <!--            <div class="sp_item" @mouseenter="showSpKs" @mouseleave="showSpKs">-->
      <!--              <div class="mask">-->
      <!--                <img :src="require('@/assets/img/24gf-play (1).png')" alt=""/>-->
      <!--              </div>-->
      <!--            </div>-->
      <!--            <div class="sp_title">2023-1-1</div>-->

      <!--          </div>-->
      <!--          <div class="sp_item_box">-->
      <!--            <div class="sp_item" @mouseenter="showSpKs" @mouseleave="showSpKs">-->
      <!--              <div class="mask">-->
      <!--                <img :src="require('@/assets/img/24gf-play (1).png')" alt=""/>-->
      <!--              </div>-->
      <!--            </div>-->
      <!--            <div class="sp_title">2023-1-1</div>-->

      <!--          </div>-->

      <!--        </div>-->
      <!--      </div>-->
    </div>
  </div>
</template>

<style>
.carousel {
  .el-dialog__body {
    padding-top: 0;
  }
}

.p_cont_box {
  background-color: white;
  padding-top: 10px;
  display: flex;
  flex-direction: column;
}

.p_cont_top {
  height: 50px;
  line-height: 50px;
  border-bottom: 3px solid #f1f2f5;
  width: 95%;
  font-weight: bold;
  margin: 0 auto;
  color: #7e8a8c;

}

.p_cont_top_left {
  float: left;
  display: flex;
  width: 80px;
  /*width: 150px;*/
  justify-content: space-between;
}

.p_menu {
  height: 100%;
  cursor: pointer;
}

.p_menu_c {
  color: black;
  border-bottom: 3px solid #409EFF;
}

.p_menu:hover {
  color: black;
  border-bottom: 3px solid #409EFF;
}

.p_cont_top_right {
  float: right;
  width: 80px;
  cursor: pointer;
}

.p_cont_top_right span:hover {
  color: #f44336;
}

.p_cont {
  padding: 20px 40px;

}

.sp_box {
  display: flex;
  flex-direction: column;
}

.sp_item_box {
  float: left;
  margin-right: 5px;
  margin-top: 20px;
  border-radius: 10px;
  background-color: #f1f2f5;
  width: 250px;
  height: 290px;


}

.sp_item {
  position: relative;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  width: 250px;
  height: 250px;
  background-color: black;

  .mask {
    display: none;
    position: absolute;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    background-color: rgba(0, 0, 0, 0.82);
    text-align: center;
    line-height: 270px;
  }

  .mask img {
    margin-top: 35%;
    height: 80px;
    width: 80px;
  }
}

.sp_title {
  width: 250px;
  height: 40px;
  text-align: center;
  line-height: 40px;
}

.zp_box {
  display: flex;
  flex-direction: column;
}

.zp_left {
  color: #7e8a8c;
  font-size: 18px;
  width: 80px;
  float: left;
  text-align: right;
}

.zp_right {
  float: left;
  margin-left: 20px;
  font-size: 18px;
  display: flex;
  flex-direction: column;
}

.zp_right a {
  color: #409EFF;
}

.zp_right a:hover {
  text-decoration: underline;
}

.one_pic {
  margin-top: 15px;
  background-color: #222327;
  border-radius: 10px;
  height: 300px;
  width: 610px;
  display: flex;
  justify-content: center;
  align-items: center;
}


.many_pic {
  margin-top: 15px;
  width: 930px;
}

.m_pic_box {
  margin-top: 10px;
  width: 300px;
  height: 200px;
  background-color: #222327;
  border-radius: 10px;
  float: left;
  margin-right: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.one_pic img, .m_pic_box img {
  max-height: 100%;
  max-width: 100%;
  cursor: url("../../assets/img/搜索 (1).png") 16 16, auto;
}


.del_jl {
  /*color: #409EFF;*/
  width: 30px;
  margin-top: 10px;
  /*font-size: 15px;*/
  margin-bottom: 20px;
}

.del_jl:hover {
  color: #f44336;
}


.xc_box {
  display: flex;
  flex-direction: column;
}

.p_ct {
  width: 400px;
  display: flex;
  justify-content: space-between;
}

.p_ct .btn {
  height: 40px;
  width: 110px;
}

.up_btn {
  background-color: #409EFF;
  color: #ffffff;
  height: 40px;
  width: 130px;
  border: none;
  border-radius: 4px;
  font-size: 15px;
  cursor: pointer;
}

.p_pic_box {
}

.pic_card {
  margin-top: 20px;
  float: left;
  margin-left: 15px;
  height: 260px;
  width: 240px;

  text-align: center;
}

.pic_card_img_box {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  margin-bottom: 5px;
  height: 200px;
  width: 200px;

  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    background-image: linear-gradient(to bottom right, transparent, rgba(0, 0, 0, 0.82));
  }
}

.pic_card_img_box img {
  max-height: 100%;
  max-width: 100%;
}

.xl_btn {
  display: none;
  margin-left: 160px;
  margin-top: 15px;
}

.xl_btn i {
  color: #f1f2f5;
  font-size: 25px;
}


.pic_zs {
  color: #f1f2f5;
  font-weight: bold;
  font-size: 30px;
  position: absolute;
  bottom: 10px;
  right: 20px;
}


.fk_box {
  border-radius: 10px;
  background-color: #f1f2f5;
  width: 95%;
  margin: 30px auto 35px;
  padding-bottom: 20px;

}

.fk_box_top {
  margin: 20px auto 0;
  width: 95%;
  font-size: 18px;
  font-weight: bold;

}

.fk_box_top span {
  margin-left: 8px;
  font-size: 15px;
  color: #7e8a8c;
}

.fk_tx_box {
  .el-avatar {
    height: 60px;
    width: 60px;
  }
}

.fk_tx {
  height: 60px;
  float: left;
  margin-top: 20px;
  margin-left: 20px;
  width: 60px;
  position: relative;

  .mask {
    position: absolute;
    border-radius: 5px;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    font-weight: bold;
    background-color: rgba(0, 0, 0, 0.3);

  }

}

.fk_nick {
  color: #f1f2f5;
  text-align: center;
  margin-top: 40px;
  bottom: 3px;
  font-size: 10px;
}


.no_value {
  justify-content: center;
  align-items: center;
  color: #7e8a8c;
  display: flex;
  margin-top: 80px;
  font-size: 35px;
  flex-direction: column;
  font-weight: bold;
  text-align: center;
  padding-bottom: 50px;
}

.no_value img {
  margin: 25px auto;
  height: 220px;

}

.no_value button {
  background-color: #409EFF;
  color: #ffffff;
  height: 60px;
  width: 200px;
  border: none;
  border-radius: 15px;
  font-size: 20px;
  cursor: pointer;
}

.no_value button:hover, .up_btn:hover {
  background-color: #4076ff;
}
</style>